<?php

namespace App\Services;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class UploadService
{
    /**
     * 上传图片
     * @param Request $request
     * @param string $file
     * @return array
     */
    public function upload(Request $request, string $file = 'file') : array
    {
        $request->validate([
            'file' => 'required|mimes:jpeg,jpg,png,webp|max:1024',
            'directory' => Rule::in(['covers', 'pics', 'retouch'])
        ], [
            'file.required' => '请选择要上传的图片',
            'file.mimes' => '不合法的图片格式',
            'file.max' => '上传图片不能超过1M',
            'directory.in' => '上传目录不合法',
        ]);

        $directory = $request->directory ?? 'covers';
        $path = $request->file($file)->store($directory . '/' . auth()->id(), 'public');

        return [
            'path' => $path,
            'url' => url('storage/' . $path)
        ];
    }
}
